package com.golxen.framework.user.player;


import com.golxen.framework.ClientProtocol;
import com.golxen.framework.activity.ActivityModule;
import com.golxen.framework.module.AbstractModule;
import com.golxen.framework.user.UserMgr;
import com.golxen.framework.user.dao.UserBussiness;
import com.golxen.framework.user.dao.entity.UserInfo;
import com.golxen.protojava.PlayerProto;


public class PlayerModule extends AbstractModule {

	private UserInfo userInfo;


	public PlayerModule(GamePlayer player) {
		super(player);
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	@Override
	public boolean loadDB() {
		userInfo = player.getUserInfo();
		return true;
	}

	@Override
	public boolean afterLoadDB() {
		return true;
	}

	@Override
	public boolean saveDB() {
		if (null != userInfo && userInfo.isUpdateOption()) {
			UserBussiness.updateUserInfo(userInfo);
		}

		return true;
	}


	@Override
	public void loginSendMsg() {
		PlayerProto.PlayerBaseTempMsg.Builder msg=PlayerPb.parsePlayerBaseTempMsg(userInfo);
		player.sendPacket(ClientProtocol.U_PLAYER_DATA, msg);

		// TODO:登录需要发送给客户端的数据

	}

	@Override
	public void afterLogin() {
		// TODO: 登录后需要发送给客户端的数据

	}


	public UserInfo getUserInfo() {
		return userInfo;
	}


	public void setNickName(String nickName) {
		userInfo.setNickName(nickName);
		onUserInfoChange();
	}


	public void onUserInfoChange() {
		// TODO：玩家信息改变需要同时改变的东西

		UserMgr.addNeedUpdateToCrossMap(player.getUserId(), userInfo);
	}


}
